<?php
class Pageley_Commerce_PlaceOrderTests extends Pageley_Commerce_DomainTests {

	public function setUp() {
		parent::setUp();
		$this->clearOrders();
	}
	
    public function tearDown() {
        parent::tearDown();
        $this->clearOrders();
    }
    
    private function clearOrders() {
        $this->dispatchRequest(array(
            "controller" => "Query",
            "typeName" => "commerce.OrderItem"
        ));
        $orders = $_REQUEST["entities"];
        foreach($orders as $order) {
            $this->dispatchRequest(array(
                "controller" => "Delete",
                "typeName" => "commerce.OrderItem",
                "id" => $order->getId()
            ));
        }
        $this->dispatchRequest(array(
            "controller" => "Query",
            "typeName" => "commerce.Order"
        ));
        $orders = $_REQUEST["entities"];
        foreach($orders as $order) {
            $this->dispatchRequest(array(
                "controller" => "Delete",
                "typeName" => "commerce.Order",
                "id" => $order->getId()
            ));
        }
    }

    public function testPlaceOrderAndCancelOrder() {
        $success = $this->dispatchRequest(array(
            "controller" => "Commerce/LogInCustomer",
            "username" => "bill.gates",
            "password" => "irock"
        ));
        $this->assertTrue($success);
        $success = $this->dispatchRequest(array(
            "controller" => "Commerce/AddItemToOrder",
            "articleId" => "1",
            "quantity" => "2"
        ));
        $this->assertTrue($success);
        $success = $this->dispatchRequest(array(
            "controller" => "Commerce/AddInfoToOrder",
            "paymentMethod" => "TRANSFER",
            "comment" => "test comment",
            "sendInvoice" => "1",
            "billingName" => "Bill Gates",
            "billingAddress.line1" => "1, Test Street",
            "billingAddress.postalCode" => "123456",
            "billingAddress.city" => "Test City",
            "billingEmailAddress" => "bill.gates@hotmail.com",
            "deliveryName" => "Bill Gates",
            "deliveryAddress.line1" => "1, Test Street",
            "deliveryAddress.postalCode" => "123456",
            "deliveryAddress.city" => "Test City"
        ));
        $this->assertTrue($success);
        $success = $this->dispatchRequest(array(
            "controller" => "Commerce/PlaceOrder"
        ));
        $this->assertTrue($success);
        $success = $this->dispatchRequest(array(
            "controller" => "Query",
            "typeName" => "commerce.Order",
            "propertyNames" => array("status"),
            "operators" => array("="),
            "values" => array("PLACED")
        ));
        $this->assertTrue($success);
        $orders = $_REQUEST["entities"];
        $this->assertNotNull($orders);
        $this->assertEquals(1, $orders->size());
        $order = $orders->get(0);
        $this->assertNotNull($order);
        $orderItems = $order->get("orderItems");
        $this->assertNotNull($orderItems);
        $this->assertEquals(1, $orderItems->size());
        $orderItem = $orderItems->get(0);
        $this->assertEquals(1, $orderItem->get("article")->getId());
        $this->assertEquals(2, $orderItem->get("quantity"));
        $success = $this->dispatchRequest(array(
            "controller" => "Commerce/CancelOrder",
            "orderId" => $order->getId()
        ));
        $this->assertTrue($success);
        $success = $this->dispatchRequest(array(
            "controller" => "Query",
            "typeName" => "commerce.Order",
            "propertyNames" => array("status"),
            "operators" => array("="),
            "values" => array("PLACED")
        ));
        $this->assertTrue($success);
        $orders = $_REQUEST["entities"];
        $this->assertNotNull($orders);
        $this->assertEquals(0, $orders->size());
    }
}